package cn.scw.framework.task;

import cn.scw.project.facility.domain.FacPrintOrder;
import cn.scw.project.facility.service.IFacPrintOrderService;
import cn.scw.project.system.service.ISysPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.StopWatch;

import java.util.List;

/**
 * 分账定时任务调度
 *
 * @author api
 */
@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class ProfitsharingTask {
    @Autowired
    private ISysPayService iSysPayService;

    @Autowired
    private IFacPrintOrderService iFacPrintOrderService;

    //3.添加定时任务
    @Scheduled(cron = "${cron.shareOrdertask}")
    //或直接指定时间间隔，例如：5秒
    //@Scheduled(fixedRate=5000)
    private void configureTasks() {
        StopWatch sw = new StopWatch();
        //获取支付成功并且还没有完成分账的订单，30天以内的
        sw.start();
        System.out.println("---开始执行分账任务---线程ID="+Thread.currentThread().getId());
        List<FacPrintOrder> list = iFacPrintOrderService.selectUnSharingFacPrintOrder();
        for(FacPrintOrder order : list){
            try {
                iSysPayService.shareOrder(order);
            }catch (Exception e){
                System.out.println("分账执行失败，orderId="+order.getId());
                e.printStackTrace();
            }

        }
        sw.stop();
        System.out.println("---结束执行分账任务---线程ID="+Thread.currentThread().getId()+",执行时间为："+sw.getTotalTimeMillis()+"ms");
    }

}
